// 生产模式

const { merge } = require("webpack-merge");

const commonConfig = require("./webpack.common");

const { CleanWebpackPlugin } = require("clean-webpack-plugin");

const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const HtmlWebpackPlugin = require("html-webpack-plugin");

const OptimizeCssAssetsWebpackPlugin = require("optimize-css-assets-webpack-plugin");

const TerserWebpackPlugin = require("terser-webpack-plugin");

const path = require("path");

// 生产模式

module.exports = merge(commonConfig, {
  mode: "production",

  output: {
    filename: "[name].[chunkhash:8].js",

    path: path.resolve(__dirname, "dist"),
  },

  //提取公共代码、压缩js和css代码

  optimization: {
    // 提取公共模块

    splitChunks: {
      chunks: "all",
    },

    // 压缩js css代码

    minimize: true,

    minimizer: [
      new TerserWebpackPlugin(),

      new OptimizeCssAssetsWebpackPlugin(),
    ],
  },

  module: {
    rules: [
      // 分离css文件

      {
        test: /\.less$/,

        use: [MiniCssExtractPlugin.loader, "css-loader", "less-loader"],
      },

      {
        test: /\.css$/,

        use: [MiniCssExtractPlugin.loader, "css-loader"],
      },
    ],
  },

  plugins: [
    new CleanWebpackPlugin(),

    new HtmlWebpackPlugin({
      template: "./public/index.html",

      title: "jack webapck",

      favicon: "./public/favicon.ico",

      minify: {
        removeComments: true, // 去注释

        collapseWhitespace: true, // 去空格

        removeAttributeQuotes: true, // 去属性引用
      },
    }),

    // 分离css文件

    new MiniCssExtractPlugin({
      filename: "[name].[chunkhash:8].css",
    }),
  ],
});
